package com.gxp.controller.siteinfo;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gxp.pojo.SiteSetting;
import com.gxp.pojo.SiteSettingForm;
import com.gxp.service.SiteSettingService;
import com.gxp.util.common.GXPConstant;
import com.gxp.util.common.GXPUtil;
import com.gxp.util.common.GXPWebUtil;
import com.gxp.util.security.Des;
import com.gxp.validator.SiteSettingValidator;


@Controller
public class SiteSettingController {
	
	Logger logger = Logger.getLogger(SiteSettingController.class);

	@Autowired
	private SiteSettingService siteSettingService;
	
	@InitBinder
	protected void initBinder(WebDataBinder binder){
		binder.setValidator(new SiteSettingValidator());
	}


	@RequestMapping(value="/admin/siteinfo/edit",method=RequestMethod.GET)
	public ModelAndView editSiteINfo() throws Exception {
		
		logger.debug("SiteInfoController editSiteINfo is invoked.........");
		
		Map<String, String> settings = siteSettingService.getSiteSetting();
		logger.debug("settings size: " +  settings.size());
		String pwd = settings.get(GXPConstant.SITE_SETTING_KEY_EMAIL_PASSWORD);
		if (StringUtils.hasText(pwd)) {
			settings.put(GXPConstant.SITE_SETTING_KEY_EMAIL_PASSWORD, Des.decrypt(pwd));
		}
		ModelAndView mav = new ModelAndView();
		mav.addObject("settings", settings);
		mav.setViewName("admin/siteinfo/siteInfo");
		return mav;
	}

	@RequestMapping(value="/admin/site/update",method=RequestMethod.POST, produces="application/json;charset=UTF-8")
	@ResponseBody
//	@Secured({"ROLE_ADMIN_ALL","ROLE_ADMIN_SITE"})
	@PreAuthorize("hasAnyRole('ROLE_ADMIN_ALL','ROLE_ADMIN_SITE') and hasRole('PERM_SITE_UPDATE')")
	public String saveSiteSetting(@Valid SiteSettingForm siteSettingForm, BindingResult errors, HttpServletRequest req, HttpServletResponse resp) throws Exception {
		
		if (errors.hasErrors()) {
			return GXPWebUtil.handleErrorResponse(errors.getAllErrors(), req, resp);
		}
		logger.debug("SiteInfoController saveSiteSetting is invoked.........");
		
		Enumeration<String> enums = req.getParameterNames();  
	    while (enums.hasMoreElements()) {
	        String key = enums.nextElement();  
	        String content = req.getParameter(key);
	        if (GXPConstant.SITE_SETTING_KEY_EMAIL_PASSWORD.equals(key)) {
	        	content = Des.encrypt(content);
	        }
	        logger.debug("key : " + key);
			logger.debug("content : " + content);
			SiteSetting setting = new SiteSetting();
			setting.setKey(key);
			setting.setContent(content);
			siteSettingService.updateSiteSetting(setting);
			req.getServletContext().setAttribute(key, content);
	    }
	   
		return GXPUtil.getJsonString("success", false, "");
	}

}
